home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Macintosh Tracker 1.1 Source / Original Tracker 3.10 Source / commands.c < prev    next >
Text File  |  1993-05-22  |  12KB  |  514 lines

  1. /* commands.c */
  2.  
  3. /* $Id: commands.c,v 3.7 1993/01/15 14:00:28 espie Exp espie $
  4.  * $Log: commands.c,v $
  5.  * Revision 3.7  1993/01/15  14:00:28  espie
  6.  * Added bg/fg test.
  7.  *
  8.  * Revision 3.6  1992/11/27  10:29:00  espie
  9.  * General cleanup
  10.  *
  11.  * Revision 3.5  1992/11/24  10:51:19  espie
  12.  * More precise vibrato table.
  13.  *
  14.  * Revision 3.4  1992/11/23  10:12:23  espie
  15.  * *** empty log message ***
  16.  *
  17.  * Revision 3.3  1992/11/22  17:20:01  espie
  18.  * Simplified delay_pattern.
  19.  *
  20.  * Revision 3.2  1992/11/20  14:53:32  espie
  21.  * Added finetune.
  22.  *
  23.  * Revision 3.1  1992/11/19  20:44:47  espie
  24.  * Protracker commands.
  25.  *
  26.  * Revision 3.0  1992/11/18  16:08:05  espie
  27.  * New release.
  28.  *
  29.  * Revision 2.12  1992/11/13  13:24:24  espie
  30.  * Added some extended commands: E12AB, and some.
  31.  * now use set_volume in audio.c. All the device-dependent operation
  32.  * is there.
  33.  * Defensive programming: check the range of each note
  34.  * for arpeggio setup.
  35.  * Structured part of the code, especially replay ``automaton''
  36.  * and setting up of effects.
  37.  *
  38.  * Revision 1.9  1991/11/17  17:09:53  espie
  39.  * Added missing prototypes.
  40.  * Dynamic oversample and frequency.
  41.  * Added arpeggio.
  42.  * Fixed up vibrato depth.
  43.  * Added vibslide and portaslide.
  44.  * Added command 9.
  45.  */
  46.  
  47. #include <stdio.h>
  48.  
  49. #include "defs.h"
  50. #include "extern.h"
  51. #include "channel.h"
  52. #include "song.h"
  53.      
  54. LOCAL char *id = "$Id: commands.c,v 3.7 1993/01/15 14:00:28 espie Exp espie $";
  55.  
  56. /* sine table for the vibrato effect */
  57.  
  58. int vibrato_table[64] = 
  59.     {
  60.     0,50,100,149,196,241,284,325,362,396,426,452,473,490,502,510,512,
  61.     510,502,490,473,452,426,396,362,325,284,241,196,149,100,50,0,-49,
  62.     -99,-148,-195,-240,-283,-324,-361,-395,-425,-451,-472,-489,-501,
  63.     -509,-511,-509,-501,-489,-472,-451,-425,-395,-361,-324,-283,-240,
  64.     -195,-148,-99,-49
  65.     };
  66.  
  67. /***
  68.  *
  69.  *
  70.  *  setting up effects/doing effects.
  71.  *  The set_xxx gets called while parsing the effect,
  72.  *  the do_xxx gets called each tick, and update the
  73.  *  sound parameters while playing it.
  74.  *
  75.  *
  76.  ***/
  77.  
  78.  
  79. void do_nothing(ch)
  80. struct channel *ch;
  81.     {
  82.     }
  83.  
  84. void set_nothing(a, ch)
  85. struct automaton *a;
  86. struct channel *ch;
  87.     {
  88.     }
  89.  
  90. /* slide pitch (up or down) */
  91. void do_slide(ch)
  92. struct channel *ch;
  93.     {
  94.     ch->pitch += ch->slide;
  95.     ch->pitch = MIN(ch->pitch, MAX_PITCH);
  96.     ch->pitch = MAX(ch->pitch, MIN_PITCH);
  97.     set_current_pitch(ch, ch->pitch);
  98.     }
  99.  
  100. void set_upslide(a, ch)
  101. struct automaton *a;
  102. struct channel *ch;
  103.     {
  104.     ch->adjust = do_slide;
  105.     if (a->para)
  106.         ch->slide = a->para;
  107.     }
  108.  
  109. void set_downslide(a, ch)
  110. struct automaton *a;
  111. struct channel *ch;
  112.     {
  113.     ch->adjust = do_slide;
  114.     if (a->para)
  115.         ch->slide = -a->para;
  116.     }
  117.  
  118. /* modulating the pitch with vibrato */
  119. void do_vibrato(ch)
  120. struct channel *ch;
  121.     {
  122.     int offset;
  123.  
  124.         /* this is no longer a literal transcription of the pt
  125.          * code. I have rescaled the vibrato table.
  126.          */
  127.     ch->viboffset += ch->vibrate;
  128.     ch->viboffset &= 63;
  129.         /* please don't use logical shift on signed values */
  130.     offset = (vibrato_table[ch->viboffset] * ch->vibdepth)/256;
  131.         /* temporary update of only the step value,
  132.          * note that we do not change the saved pitch.
  133.          */
  134.     set_current_pitch(ch, ch->pitch + offset);
  135.     }
  136.  
  137. void set_vibrato(a, ch)
  138. struct automaton *a;
  139. struct channel *ch;
  140.     {
  141.     ch->adjust = do_vibrato;
  142.     if (a->para)
  143.         {
  144.         ch->vibrate = HI(a->para);
  145.         ch->vibdepth = LOW(a->para);
  146.         }
  147.     }
  148.  
  149. /* arpeggio looks a bit like chords: we alternate between two
  150.  * or three notes very fast.
  151.  * Issue: we are able to re-generate real chords. Would that be
  152.  * better ? To try.
  153.  */
  154. void do_arpeggio(ch)
  155. struct channel *ch;
  156.     {
  157.     if (++ch->arpindex >= MAX_ARP)
  158.         ch->arpindex =0;
  159.     set_current_pitch(ch, ch->arp[ch->arpindex]);
  160.     }
  161.  
  162. void set_arpeggio(a, ch)
  163. struct automaton *a;
  164. struct channel *ch;
  165.     {
  166.         /* normal play is arpeggio with 0/0 */
  167.     if (!a->para)
  168.         return;
  169.         /* arpeggio can be installed relative to the
  170.          * previous note, so we have to check that there
  171.          * actually is a current(previous) note
  172.          */
  173.     if (ch->note == NO_NOTE)
  174.         {
  175.         fprintf(stderr,
  176.             "No note present for arpeggio");
  177.         error = FAULT;
  178.         }
  179.     else
  180.         {
  181.         int note;
  182.  
  183.         ch->arp[0] = pitch_table[ch->note][ch->finetune];
  184.         note = ch->note + HI(a->para);
  185.         if (note < NUMBER_NOTES)
  186.             ch->arp[1] = pitch_table[note][ch->finetune];
  187.         else
  188.             {
  189.             fprintf(stderr,
  190.                 "Arpeggio note out of range");
  191.             error = FAULT;
  192.             }
  193.         note = ch->note + LOW(a->para);
  194.         if (note < NUMBER_NOTES)
  195.             ch->arp[2] = pitch_table[note][ch->finetune];
  196.         else
  197.             {
  198.             fprintf(stderr,
  199.                 "Arpeggio note out of range");
  200.             error = FAULT;
  201.             }
  202.         ch->arpindex = 0;
  203.         ch->adjust = do_arpeggio;
  204.         }
  205.     }
  206.  
  207. /* volume slide. Mostly used to simulate waveform control.
  208.  * (attack/decay/sustain).
  209.  */
  210. void do_slidevol(ch)
  211. struct channel *ch;
  212.     {
  213.     ch->volume += ch->volumerate;
  214.     set_current_volume(ch, ch->volume);
  215.     }
  216.  
  217. /* note that volumeslide does not have a ``take default''
  218.  * behavior. If para is 0, this is truly a 0 volumeslide.
  219.  * Issue: is the test really necessary ? Can't we do
  220.  * a HI(para) - LOW(para). Answer: protracker does not.
  221.  */
  222. void parse_slidevol(ch, para)
  223. struct channel *ch;
  224. int para;
  225.     {
  226.     if (LOW(para))
  227.         ch->volumerate = -LOW(para);
  228.     else
  229.         ch->volumerate = HI(para);
  230.     }
  231.  
  232. void set_slidevol(a, ch)
  233. struct automaton *a;
  234. struct channel *ch;
  235.     {
  236.     ch->adjust = do_slidevol;
  237.     parse_slidevol(ch, a->para);
  238.     }
  239.  
  240. /* extended command: retrig note at a fast pace
  241.  */
  242. void do_retrig(ch)
  243. struct channel *ch;
  244.     {
  245.     if (--ch->current <= 0)
  246.         {
  247.         reset_note(ch, ch->note, ch->pitch);
  248.         ch->current = ch->retrig;
  249.         }
  250.     }
  251.  
  252. /* extended command: start note after a small
  253.  * delay
  254.  */
  255. void do_latestart(ch)
  256. struct channel *ch;
  257.     {
  258.     if (--ch->current <= 0)
  259.         {
  260.         reset_note(ch, ch->note, ch->pitch);
  261.         ch->adjust = do_nothing;
  262.         }
  263.     }
  264.  
  265. /* extended command: cut note after some time.
  266.  * Note we only kill the volume.
  267.  */
  268. void do_cut(ch)
  269. struct channel *ch;
  270.     {
  271.     if (ch->retrig)
  272.         {
  273.         if (--ch->retrig == 0)
  274.             set_current_volume(ch, 0);
  275.         }
  276.     }
  277.  
  278. void set_extended(a, ch)
  279. struct automaton *a;
  280. struct channel *ch;
  281.     {
  282.     switch(HI(a->para))
  283.         {
  284.     case 0:
  285.         break;
  286.     case 1:
  287.         ch->pitch += LOW(a->para);
  288.         ch->pitch = MIN(ch->pitch, MAX_PITCH);
  289.         ch->pitch = MAX(ch->pitch, MIN_PITCH);
  290.         set_current_pitch(ch, ch->pitch);
  291.         break;
  292.     case 2:
  293.         ch->pitch -= LOW(a->para);
  294.         ch->pitch = MIN(ch->pitch, MAX_PITCH);
  295.         ch->pitch = MAX(ch->pitch, MIN_PITCH);
  296.         set_current_pitch(ch, ch->pitch);
  297.         break;
  298.     case 5:
  299.         ch->finetune = LOW(a->para);
  300.         break;
  301.     case 6:
  302.         /* Note: the current implementation of protracker
  303.          * does not allow for a jump from pattern to pattern,
  304.          * but it looks like a logical extension to the current 
  305.          * format.
  306.          */
  307.         if (LOW(a->para) == 0) 
  308.             {
  309.             a->loop_pattern_num = a->pattern_num;
  310.             a->loop_note_num = a->note_num;
  311.             }
  312.         else
  313.             {
  314.             if (a->loop_counter == 0)
  315.                 a->loop_counter = LOW(a->para);
  316.             else
  317.                 a->loop_counter--;
  318.             if (a->loop_counter > 0)
  319.                 a->do_stuff |= JUMP_PATTERN;
  320.             }
  321.         break;
  322.     case 9:
  323.         ch->retrig = LOW(a->para);
  324.         ch->current = ch->retrig;
  325.         ch->adjust = do_retrig;
  326.         break;
  327.     case 10:
  328.         ch->volume += LOW(a->para);
  329.         break;
  330.     case 11:
  331.         ch->volume -= LOW(a->para);
  332.         break;
  333.     case 12:
  334.         ch->retrig = LOW(a->para);
  335.         ch->adjust = do_cut;
  336.         break;
  337.     case 13:
  338.         ch->mode = DO_NOTHING;
  339.         ch->current = LOW(a->para);
  340.         ch->adjust = do_latestart;
  341.         break;
  342.     case 14:
  343.         a->counter -= (LOW(a->para) + 1) * a->speed;
  344.         a->do_stuff |= DELAY_PATTERN;
  345.         break;
  346.     default:
  347.         fprintf(stderr, "Not supported %d-%d\n", HI(a->para), LOW(a->para));
  348.         break;
  349.         }
  350.     }
  351.         
  352. /* portamento: gets from a given pitch to another.
  353.  * We can simplify the routine by cutting it in
  354.  * a pitch up and pitch down part while setting up
  355.  * the effect.
  356.  */
  357. void do_portamento(ch)
  358. struct channel *ch;
  359.     {
  360.     if (ch->pitch < ch->pitchgoal)
  361.         {
  362.         ch->pitch += ch->pitchrate;
  363.         ch->pitch = MIN(ch->pitch, ch->pitchgoal);
  364.         }
  365.     else if (ch->pitch > ch->pitchgoal)
  366.         {
  367.         ch->pitch -= ch->pitchrate;
  368.         ch->pitch = MAX(ch->pitch, ch->pitchgoal);
  369.         }
  370.         /* if we want to implement funk glissando, we need a change right
  371.          * there
  372.          */
  373.     set_current_pitch(ch, ch->pitch);
  374.     }
  375.  
  376. /* if para and pitch are 0, this is obviously a continuation
  377.  * of the previous portamento.
  378.  */
  379. void set_portamento(a, ch)
  380. struct automaton *a;
  381. struct channel *ch;
  382.     {
  383.     ch->adjust = do_portamento;
  384.     if (a->para)
  385.         ch->pitchrate = a->para;
  386.     if (a->pitch)
  387.         ch->pitchgoal = a->pitch;
  388.     }
  389.  
  390. /*
  391.  * combined commands.
  392.  */
  393. void do_portaslide(ch)
  394. struct channel *ch;
  395.     {
  396.     do_portamento(ch);
  397.     do_slidevol(ch);
  398.     }
  399.  
  400. void set_portaslide(a, ch)
  401. struct automaton *a;
  402. struct channel *ch;
  403.     {
  404.     ch->adjust = do_portaslide;
  405.     if (a->pitch)
  406.         ch->pitchgoal = a->pitch;
  407.     parse_slidevol(ch, a->para);
  408.     }
  409.  
  410. void do_vibratoslide(ch)
  411. struct channel *ch;
  412.     {
  413.     do_vibrato(ch);
  414.     do_slidevol(ch);
  415.     }
  416.  
  417. void set_vibratoslide(a, ch)
  418. struct automaton *a;
  419. struct channel *ch;
  420.     {
  421.     ch->adjust = do_vibratoslide;
  422.     parse_slidevol(ch, a->para);
  423.     }
  424.  
  425. /***
  426.  *
  427.  *  effects that just need a setup part
  428.  *
  429.  ***/
  430.  
  431. /* IMPORTANT: because of the special nature of
  432.  * the player, we can't process each effect independently,
  433.  * we have to merge effects from the four channel before
  434.  * doing anything about it. For instance, there can be 
  435.  * several speed change in the same note,
  436.  * only the last one takes effect.
  437.  */
  438. void set_speed(a, ch)
  439. struct automaton *a;
  440. struct channel *ch;
  441.     {
  442.     a->new_speed = a->para;
  443.     a->do_stuff |= SET_SPEED;
  444.     }
  445.  
  446. void set_skip(a, ch)
  447. struct automaton *a;
  448. struct channel *ch;
  449.     {
  450.         /* yep, this is BCD. */
  451.     a->new_note = HI(a->para) * 10 + LOW(a->para);
  452.     a->do_stuff |= SET_SKIP;
  453.     }
  454.  
  455. void set_fastskip(a, ch)
  456. struct automaton *a;
  457. struct channel *ch;
  458.     {
  459.     a->new_pattern = a->para;
  460.     a->do_stuff |= SET_FASTSKIP;
  461.     }
  462.  
  463. /* immediate effect: starts the sample somewhere
  464.  * off the start.
  465.  */
  466. void set_offset(a, ch)
  467. struct automaton *a;
  468. struct channel *ch;
  469.     {
  470.     ch->pointer = int_to_fix(a->para * 256);
  471.     }
  472.  
  473. /* change the volume of the current channel.
  474.  * Is effective until there is a new set_volume,
  475.  * slide_volume, or an instrument is reloaded 
  476.  * explicitly by giving its number. Obviously, if
  477.  * you load an instrument and do a set_volume in the
  478.  * same note, the set_volume will take precedence.
  479.  */
  480. void set_volume(a, ch)
  481. struct automaton *a;
  482. struct channel *ch;
  483.     {
  484.     set_current_volume(ch, a->para);
  485.     }
  486.  
  487.  
  488.  
  489.  
  490.  
  491. /* Initialize the whole effect table */
  492.  
  493. void init_effects(table)
  494. void (*table[])();
  495.     {
  496.     table[0] = set_arpeggio;
  497.     table[15] = set_speed;
  498.     table[13] = set_skip;
  499.     table[11] = set_fastskip;
  500.     table[12] = set_volume;
  501.     table[10] = set_slidevol;
  502.     table[9] = set_offset;
  503.     table[3] = set_portamento;
  504.     table[5] = set_portaslide;
  505.     table[2] = set_upslide;
  506.     table[1] = set_downslide;
  507.     table[4] = set_vibrato;
  508.     table[6] = set_vibratoslide;
  509.     table[14] = set_extended;
  510.     table[7] = set_nothing;
  511.     table[8] = set_nothing;
  512.     }
  513.  
  514.